package com.example.springboot_system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot_system.mapper.AnnounceMapper;
import com.example.springboot_system.mapper.DrugMapper;
import com.example.springboot_system.mapper.UserMapper;
import com.example.springboot_system.pojo.Announce;
import com.example.springboot_system.pojo.Drug;
import com.example.springboot_system.pojo.User;
import com.example.springboot_system.service.DrugsService;
import com.example.springboot_system.service.UserService;
import com.example.springboot_system.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class DrugsSeviceImpl implements DrugsService {
    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private AnnounceMapper announceMapper;

    @Autowired(required = false)
    private DrugMapper drugMapper;


    //得到所有药物数据
    @Override
    public List<Drug> getAllDrugs(HttpSession session, Model model) {
        QueryWrapper<Drug> queryWrapper = new QueryWrapper<>();
        List<Drug> list = drugMapper.selectList(queryWrapper);
        System.out.println("业务层查询全部药物数据：" + list);
        return list;
    }

    //得到一个药物信息
    @Override
    public Drug getTheDrugs(String id) {
        Drug drug = drugMapper.selectById(id);
        return drug;
    }

    //更新药物信息
    @Override
    public ResponseResult updateDrugs(Drug drug) {
        QueryWrapper<Drug> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", drug.getId());
        int row = drugMapper.update(drug, queryWrapper);
        if (row != 1) {
            ResponseResult result = new ResponseResult(202, "更新失败", row);
            return result;
        }
        ResponseResult result = new ResponseResult(201, "更新成功", row);
        return result;

    }

    //删除药品
    @Override
    public ResponseResult deleteDrug(String id) {
        int row = drugMapper.deleteById(id);
        if (row != 1) {
            ResponseResult result = new ResponseResult(202, "删除失败", row);
            return result;
        }
        ResponseResult result = new ResponseResult(201, "删除成功", row);
        return result;
    }

    //查询全部药品名
    @Override
    public ResponseResult findAllDrugsName() {
        QueryWrapper<Drug> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT name");
        List<Drug> drugsName = drugMapper.selectList(queryWrapper);
        if (drugsName == null) {
            ResponseResult result = new ResponseResult(202, "查询失败");
            return result;
        }
        ResponseResult result = new ResponseResult(201, "查询成功", drugsName);
        return result;
    }

    //查询全部药品效果
    @Override
    public ResponseResult findAllDrugsFunction() {
        QueryWrapper<Drug> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT efficacy_classification");
        List<Drug> efficacyClassification = drugMapper.selectList(queryWrapper);
        if (efficacyClassification == null) {
            ResponseResult result = new ResponseResult(202, "查询失败");
            return result;
        }
        ResponseResult result = new ResponseResult(201, "查询成功", efficacyClassification);
        return result;
    }

    //查询全部剂型
    @Override
    public ResponseResult findAllDrugsReagent() {
        QueryWrapper<Drug> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT drug_type");
        List<Drug> drugType = drugMapper.selectList(queryWrapper);
        if (drugType == null) {
            ResponseResult result = new ResponseResult(202, "查询失败,没有该数据!!!");
            return result;
        }
        ResponseResult result = new ResponseResult(201, "查询成功", drugType);
        return result;
    }

    //特定情况查看响应的药品
    @Override
    public ResponseResult findTheDrugsBySelect(String selectdrugsname, String selectdrugsReagent, int selectLimit, String selectdrugsFunction) {
        QueryWrapper<Drug> queryWrapper = new QueryWrapper<>();
        //如果效果没有被选择
        if (selectdrugsFunction.equals("0")) {
            System.out.println("selectdrugsFunction为0");
        } else {
            queryWrapper.eq("efficacy_classification", selectdrugsFunction);
        }
        if (selectdrugsname.equals("0")) {
            System.out.println("selectdrugsname为0");
        } else {
            queryWrapper.eq("name", selectdrugsname);
        }
        if (selectdrugsReagent.equals("0")) {
            System.out.println("selectdrugsReagent为0");
        } else {
            queryWrapper.eq("drug_type", selectdrugsReagent);
        }
        if (selectLimit == 0) {
            System.out.println("selectLimit为0");
            queryWrapper.eq("limit_status", 0);
        } else if (selectLimit == 1) {
            System.out.println("selectLimit为0");
            queryWrapper.eq("limit_status", 1);
        } else {
            System.out.println("selectLimit为空");
        }
        List<Drug> drugSelect = drugMapper.selectList(queryWrapper);
        System.out.println(drugSelect);
        if (drugSelect.size() == 0) {
            ResponseResult result = new ResponseResult(202, "查询失败", drugSelect);
            return result;
        }
        ResponseResult result = new ResponseResult(201, "查询成功", drugSelect);
        return result;
    }

    //新增药品
    @Override
    public ResponseResult insertDrugs(Drug drug) {
        System.out.println("业务层新增药品：");
        String suiji = (int) ((Math.random() * 9 + 1) * 100000) + "";
      /*  drug.setProductionDate(productionDate);
        drug.setQualityDate(qualityDate);*/
        drug.setId(suiji);
        Date date = new Date();
        drug.setCreateDatetime(date);
        int row = drugMapper.insert(drug);
        if (row != 1) {
            ResponseResult result = new ResponseResult(202, "新增失败", row);
            return result;
        }
        ResponseResult result = new ResponseResult(201, "新增成功", row);
        return result;
    }

    //根据名字查询药品信息
    @Override
    public ResponseResult findDrugsByName(String name) {
        QueryWrapper<Drug> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        Drug drug = drugMapper.selectOne(queryWrapper);
        if (drug == null) {
            ResponseResult result = new ResponseResult(202, "查询失败,未发现该药名存在数据", drug);
            return result;
        }
        ResponseResult result = new ResponseResult(201, "查询成功", drug);
        return result;
    }

    //更新库存值
    @Override
    public ResponseResult updateKuCun(Integer num, String name) {
        QueryWrapper<Drug> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        //查询当前的药品信息
        Drug drug = drugMapper.selectOne(queryWrapper);
        drug.setStorageQuantity(num);

        int row = drugMapper.update(drug, queryWrapper);
        if (row != 1) {
            ResponseResult result = new ResponseResult(202, "更新失败", drug);
            return result;
        }
        ResponseResult result = new ResponseResult(201, "更新成功", drug);
        return result;
    }


}
